package com.example.demo.processor;

import com.example.demo.data.Wap;
import com.example.demo.data.WapRepository;
import com.example.demo.util.util;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.processor.PageProcessor;

import javax.annotation.Resource;

/**
 * @author Created by 陈淦彬
 * @date 2018/10/16
 */
public class MyPageProcessor implements PageProcessor {
    @Resource
    WapRepository wapRepository;

    @Override
    public void process(Page page) {
        Document parse = Jsoup.parse(page.getRawText());

        Elements lis = parse.body().getElementsByTag("li");

        for (Element li : lis) {
            Element div = li.getElementsByTag("a").get(0).getElementsByTag("div").get(0);

            String h2 = div.getElementsByTag("h2").get(0).text();
            String h4 = div.getElementsByTag("h4").get(0).text();

            String p = div.getElementsByTag("p").get(0).text();
            Elements spans = div.getElementsByTag("span");
            String span = spans.last().text();

            Wap wap = new Wap();
            wap.setMoney(span);
            wap.setName(h2);
            wap.setPage(page.getRequest().getHeaders().get("pageNo").toString());
            wap.setPhone(h4);
            wap.setText(p);

            System.out.println(wap.toString());
            wapRepository.save(wap);
            page.putField(span,wap);
        }

        page.addTargetRequest(util.getRequest());
    }

    @Override
    public Site getSite() {
        return Site.me();
    }
}
